package com.yeyks.service.impl;

import com.yeyks.dal.dao.AccountInfoMapper;
import com.yeyks.dal.dao.WebSocketLogMapper;
import com.yeyks.dal.domain.AccountInfo;
import com.yeyks.dal.domain.WebSocketLog;
import com.yeyks.service.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author fuchangshuai
 * @date 2019/4/8 10:13 AM
 */
@Slf4j
@Service
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    private AccountInfoMapper accountInfoMapper;

    @Autowired
    private WebSocketLogMapper webSocketLogMapper;

    @Override
    public int insertSelective(Integer userId, String sessionId, String type) {
        if (userId == null) {
            log.info("webSocket日志插入中用户id不能为空");
            return 0;
        }
        AccountInfo accountInfo = accountInfoMapper.selectByPrimaryKey(userId);
        if (accountInfo == null) {
            log.info("webSocket日志插入中用户不能为空userId = {}", userId);
            return 0;
        }
        WebSocketLog webSocketLog = getWebsocketLog(accountInfo, sessionId, type);
        return webSocketLogMapper.insertSelective(webSocketLog);
    }

    private WebSocketLog getWebsocketLog(AccountInfo accountInfo, String sessionId, String type) {
        WebSocketLog webSocketLog = new WebSocketLog();
        webSocketLog.setSocketTime(new Date());
        webSocketLog.setUserId(accountInfo.getId());
        webSocketLog.setSessionId(sessionId);
        webSocketLog.setUsername(accountInfo.getUsername());
        webSocketLog.setType(type);

        return webSocketLog;
    }
}
